package edu.mtu.citizenscience.streamapplication.activity.main;

import edu.mtu.citizenscience.streamapplication.activity.R;
import edu.mtu.citizenscience.streamapplication.activity.R.layout;
import edu.mtu.citizenscience.streamapplication.activity.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

/**
 * @author Bryan Becker <bcbecker@mtu.edu>
 * @copyright (c) 2013 MichiganTech <http://www.mtu.edu>
 * 
 * Tutorial Activity that displays a slide show and shows the user
 * how to use the app.
 */
public class SCTutorialActivity extends Activity implements ViewFactory  {
    
	//tutorialPics array that stores all the tutorial pics
    Integer[] tutorialPics = {R.drawable.tutorial_1, R.drawable.tutorial_1,
        	R.drawable.tutorial_1};
    
    // the current slide position starts at 0
    private int position = 0;
    
    // image switcher which will change the images
	private ImageSwitcher slideSwitcher;
	//custom header bar
	private TextView header;
	//text that shows which tutorial page we are on (ex. 1/3)
	private TextView counter;
	//next button
	private Button next;
	//prev button
	private Button prev;
	
	/**
	 * Tutorial Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//set the content view and enable the custom title
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_tutorial);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		
		//set the header of our custom title to be "Tutorial"
		header = (TextView) findViewById(R.id.header_text);
		header.setText("Tutorial");
		
		//set up our buttons and text views
		next = (Button) findViewById(R.id.next_button);
		prev = (Button) findViewById(R.id.prev_button);
		counter = (TextView) findViewById(R.id.counter);
		
        // Create ImageSwitcher to cycle through the tutorial slides
        slideSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        slideSwitcher.setFactory(this);
        
        // set the animations for removing and bringing photos
        slideSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        slideSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        
        // update page view
        this.update();
	}

	/**
	 * Make a new image view
	 * 
	 * @param   void
	 * @return	View - the new image view
	 */
	public View makeView() 
	{
		return new ImageView(this);
	}
	
	/**
	 * onClick handler for the prev button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void prev(View view)
	{
		// deincrement the image position
		this.position--;
		
		// update the page display
		this.update();
	}
	
	/**
	 * onClick handler for the next button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void next(View view)
	{
		// increment the image position
		this.position++;
		
		// update the page display
		this.update();
	}
	
	/**
	 * Update the visibility of the buttons and the currently displayed slide
	 * 
	 * @param   void
	 * @return	void
	 */
	private void update()
	{
		// check if the position is 0
		if (this.position == 0)
		{
			//set the prev button to invisible since it is the first slide
			prev.setVisibility(View.GONE);
		}
		//if the position is on the last slide
		else if (position == (tutorialPics.length - 1))
		{
			//set the next button to invisible since it is the last slide
			next.setVisibility(View.GONE);
		}
		//otherwise both buttons should be visible
		else
		{
			prev.setVisibility(View.VISIBLE);
			next.setVisibility(View.VISIBLE);
		}
		
		// change image on the switcher
		this.slideSwitcher.setImageResource(this.tutorialPics[this.position]);
		
		// set the page number text to the current image position out of all slides
		counter.setText((this.position + 1) + "/" + this.tutorialPics.length);
	}
	
	/**
	 * onClick handler for the Tutorial button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void tutorial(View view) {
		//do nothing if the tutorial button is clicked while in the tutorial activity
	}

}
